package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 李红磊
 * @since 2021-06-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;



    //添加课程信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        if (courseInfoVo == null) {
            throw new GuliException(20001, "添加课程信息失败，空数据");
        }
        
        //向课程表添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }

        //获取保存后的id，与课程描述建立关系
        String CourseId = eduCourse.getId();




        //向课程简介添加课程简介
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, description);
        description.setId(CourseId);
        eduCourseDescriptionService.save(description);

        return CourseId;
    }


    @Override//做课程修改的数据回响
    public CourseInfoVo getCourseById(String courseId) {
        //1.查询课程表

        EduCourse eduCourse = this.getById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);


        //2.查询课程描述表
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(courseDescription,courseInfoVo);

        return courseInfoVo;
    }


    @Override//修改课程
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if(i==0){
            throw new GuliException(20001,"修改失败");
        }

        //2.修改课程描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        eduCourseDescriptionService.updateById(courseDescription);

    }

    @Override
    public CoursePublishVo coursePublishInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override//删除课程
    public void removeCourse(String courseId) {
        //1.根据课程id删除小节

        eduVideoService.removeVideoByCourseId(courseId);

        //2.根据课程id删除章节

        eduChapterService.removeChapterByCourseById(courseId);

        //3.根据课程id删除描述

        eduCourseDescriptionService.removeById(courseId);
        //4.根据课程id删除课程本身

        int flag = baseMapper.deleteById(courseId);

        if(flag==0){
            throw new RuntimeException("删除失败");
        }

    }


    //课程分页+条件
    @Override
    public Map<String, Object> getCourseListService(Page<EduCourse> coursePage,
                                                    CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //先判断，再查询。动态sql
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//创建时间，最新
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格
            wrapper.orderByDesc("price");
        }



        this.page(coursePage,wrapper);

        List<EduCourse> records = coursePage.getRecords();//这一页的数据集合
        long current = coursePage.getCurrent();//当前页数
        long pages = coursePage.getPages();//总页数
        long size = coursePage.getSize();//每页展示的数据量
        long total = coursePage.getTotal();//总纪录数

        boolean hasNext = coursePage.hasNext();//当前是否还有下一页（比如如果到了最后一页，就没有下一页）
        boolean hasPrevious = coursePage.hasPrevious();//当前是否还有上一页

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }



    //根据课程id查询课程相关信息，自己编写sql
    @Override
    public CourseWebVo getCourseDetailInfo(String courseId) {
        CourseWebVo courseWebVo=  baseMapper.getCourseDetailInfo(courseId);
        return courseWebVo;
    }
}
